﻿using System.Collections;

namespace 享元模式
{
    public class FlyweightFactory
    {
        //定义一个Hashtable用于存储享元对象实现享元池
        private Hashtable flyweights=new Hashtable();
        public Flyweight GetFlyweight(string key)
        {
            //如果对象存在，则直接从享元池获取
            if (flyweights.ContainsKey(key))
            {
                return (Flyweight) flyweights[key];
            }
            //如果对象不存在，先创建一个新的对象添加到享元池中，然后返回
            Flyweight fw=new ConcreteFlyweight("state");
            flyweights.Add(key,fw);
            return fw;
        }
    }
}
